package GUI;

import Controllers.CallElevator;
import elevatorsimulator.WorldBuilder;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Control the Floor Display
 */
public class FloorUI extends JPanel {
    String UPARROW = "↑";
    String DOWNARROW = "↓";

    JLabel elevatorDoorStatusLbl;
    JLabel elevatorDirectionLbl;
    JPanel floorViewPanel = new JPanel();

    WorldBuilder wb;
    ElevatorUI eds;

    /**
     * FloorUI COntructor
     * param @wb Passing in World Builder
     * param @eds Passing in ElevatorUI
     */
    public FloorUI(WorldBuilder wb, ElevatorUI eds) {
        this.wb = wb;
        this.eds = eds;
    }

    /**
     * Displays the floor levels iteratively
     */
    public void showFloorPanels() {
        String FLOORVIEWPANEL = "Floor View";
        System.out.println("show floors panel");

        floorViewRefresh();
        for (int floorNum = 1; floorNum < wb.b.getNumFloors() + 1; floorNum++) {

            System.out.println(floorNum);
            System.out.println(wb.b.getNumFloors());

            String floorNameUS = ("Level: " + floorNum);
            String floorNameUK = ("Level: " + (floorNum - 1));

            JPanel floorElevatorStatusPanel = new JPanel();
            JPanel floorPanel = new JPanel();
            JPanel upDownPanel = new JPanel();


            JButton upBttn = new JButton(UPARROW);
            final int finalFloorNum = floorNum;
            upBttn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    new CallElevator().run(wb, finalFloorNum);
                    eds.doorFlag = false;
                    eds.soundCounter = 0;

                }
            });
            JButton downBttn = new JButton(DOWNARROW);
            downBttn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    new CallElevator().run(wb, finalFloorNum);
                    eds.doorFlag = false;
                    eds.soundCounter = 0;
                }
            });
            upDownPanel.add(upBttn, BorderLayout.NORTH);
            upDownPanel.add(downBttn, BorderLayout.SOUTH);

            elevatorDirectionLbl = new JLabel();
            elevatorDirectionLbl.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder()));
            eds.elevatorDirectionLblArray.add(floorNum - 1, elevatorDirectionLbl);

            elevatorDoorStatusLbl = new JLabel();
            elevatorDoorStatusLbl.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder()));
            eds.elevatorDoorStatusLblArray.add(floorNum - 1, elevatorDoorStatusLbl);

            floorElevatorStatusPanel.add(elevatorDirectionLbl);
            floorElevatorStatusPanel.add(elevatorDoorStatusLbl);
            floorPanel.add(upDownPanel, BorderLayout.WEST);

            if (wb.b.getBuildingZone() == 'u' && floorNum == 1) {
                floorPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Level: G"));
            } else {
                floorPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), floorNameUK));


            }
            if (wb.b.getBuildingZone() == 'a') {
                floorPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), floorNameUS));
            }
            floorPanel.add(floorElevatorStatusPanel);
            floorViewPanel.add(floorPanel);

        }
        floorViewPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), FLOORVIEWPANEL));
        add(floorViewPanel, BorderLayout.SOUTH);
        eds.add(floorViewPanel);

    }

    /**
     * Refreshes floor panels
     */
    public void floorViewRefresh() {
        floorViewPanel.removeAll();
        floorViewPanel.repaint();
        floorViewPanel.revalidate();
    }

}
